//
//  WYJPictureCollectionView.swift
//  MySinaWeibo
//
//  Created by eastriver on 16/10/17.
//  Copyright © 2016年 believewilliam. All rights reserved.
//

import UIKit
import SDWebImage
let cellID = "WYJPictureCollectionViewCell"

class WYJPictureCollectionView: UICollectionView {
    // 测试用的label
    private lazy var testLab: UILabel = {
       let lab = UILabel(title: "", textColor: UIColor.black, fontSize: 30)
        return lab
    }()
    
    // 配图的数据源数组
    var pic_urls: [WYJStatusPictureModel]? {
        didSet {
            testLab.text = "\(pic_urls?.count ?? 0)张"
//            print(testLab.frame)
//            print("~~~~~~~~~~~~~")
            reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = randomColor()
        self.dataSource = self
//        self.delegate = self
        register(WYJPictureCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        
        addSubview(testLab)
        testLab.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
     
    }

}

extension WYJPictureCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! WYJPictureCollectionViewCell
//        cell.backgroundColor = randomColor()
        cell.pic_url = pic_urls?[indexPath.item]
        
        return cell
    }
    
}
// MARK: 自定义collectionViewCell
class WYJPictureCollectionViewCell: UICollectionViewCell {
    // pictureImgV属性
    private lazy var pictureImgV: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.clipsToBounds = true
        return imgV
    }()
    // gif图标属性
    private lazy var gifImgV: UIImageView = UIImageView(image: #imageLiteral(resourceName: "timeline_image_gif"))
    // 模型属性
    var pic_url: WYJStatusPictureModel? {
        didSet {
            let urlString = pic_url?.wap_pic
//            print(urlString)
//            print("~~~~~~~~~~~~~~~~~~~~~~")
            let url = URL(string: urlString ?? "")
            pictureImgV.sd_setImage(with: url)
            gifImgV.isHidden = !(url!.absoluteString.hasSuffix("gif"))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = randomColor()
        contentView.addSubview(pictureImgV)
        pictureImgV.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        contentView.addSubview(gifImgV)
        gifImgV.snp.makeConstraints({ (make) in
            make.bottom.right.equalTo(self)
        })

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

